<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Swiper extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view("admin.swiper.index");
    }

    //获取幻灯片数据
    public function getSwiper(Request $request)
    {
        $params = $request->all();
        $limit = $params['limit'];
        $swipers = DB::table("swipers")->paginate($limit)->toArray();
        $total = DB::table("swipers")->count();
        return response()->json(['code'=>0,'data'=>$swipers['data'],'count'=>$total]);
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view("admin.swiper.add");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        ini_set("memory_limit","-1");
        $state = DB::table("swipers")->insertGetId($request->except(['_token','s']));
        if($state){
            return response()->json(['status'=>200,'msg'=>"上传成功"]);
        }else{
            return response()->json(['status'=>201,'msg'=>"上传失败"]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $swiper = DB::table('swipers')->where('id',$id)->first();
        return view('admin.swiper.edit',compact('swiper'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $state = DB::table('swipers')->where('id',$id)->update($request->except(['_token','fileselect','s']));
        if($state){
            return response()->json(['status'=>200,'msg'=>"修改成功"]);
        }else{
            return response()->json(['status'=>201,'msg'=>"修改失败"]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $state = DB::table('swipers')->where('id',$id)->delete();
        if($state){
            return response()->json(['status'=>200,'msg'=>"删除成功"]);
        }else{
            return response()->json(['status'=>201,'msg'=>"删除失败"]);
        }
    }

    public function upload(Request $request)
    {
       $file = $request->file('file');
       return uploadImg($file,"uploadImg/swiper");
    }

}
